/* IF2030 - Algorirma dan Struktur Data */
/* Kelas/Kelompok/Tahun  : 03/09/2011 */
/* Tanggal   : 2 Desember 2011 */
/* File      : tanggal.c */
/* Deskripsi : Tipe bentukan tanggal */

#include "tanggal.h"
#include <stdio.h>

/***** REALISASI FUNGSI DAN PROSEDUR *****/
date MakeDate(int x, int y, int z) {
    /* KAMUS LOKAL */
    date D;
    /* ALGORITMA */
    D.DD = x;
    D.MM = y;
    D.YY = z;
    return (D);
}

boolean IsKabisat(date D) {
    /* KAMUS LOKAL */

    /* ALGORITMA */
    return ((Year(D) %4 == 0) || ((Year(D) %100 != 0) && (Year(D) %400 == 0)));
}

boolean IsLess(date D1, date D2) {
    /* KAMUS LOKAL */

    /* ALGORITMA */
    return ((Year(D1)<Year(D2)) || ((Year(D1)==Year(D2)) && (Month(D1)<Month(D2))) ||
            ((Year(D1)==Year(D2)) && (Month(D1)==Month(D2)) && (Day(D1)<Day(D2))));
}

boolean IsSame(date D1, date D2) {
    /* KAMUS LOKAL */

    /* ALGORITMA */
    return ((Year(D1)==Year(D2)) && (Month(D1)==Month(D2)) && (Day(D1)==Day(D2)));
}

boolean IsSameDM(date D1, date D2) {
    /* KAMUS LOKAL */

    /* ALGORITMA */
    return ((Month(D1)==Month(D2)) && (Day(D1)==Day(D2)));
}

date NextDay(date D) {
    /* KAMUS LOKAL */
    date D1;
    /* ALGORITMA */
    if ((D.MM == 1) || (D.MM == 3) || (D.MM == 5) || (D.MM == 7) || (D.MM == 8) || (D.MM == 10)) {
        if (D.DD < 31) {
            D1 = MakeDate(D.DD +1, D.MM, D.YY);
        } else {
            D1 = MakeDate(1, D.MM+1, D.YY);
        }
    } else if (D.MM == 2) {
        if (IsKabisat(D)) {
            if (D.DD < 29) {
                D1 = MakeDate(D.DD + 1, D.MM, D.YY);
            } else {
                D1 = MakeDate(1, D.MM+1, D.YY);
            }
        } else {
            if (D.DD < 28) {
                D1 = MakeDate(D.DD + 1, D.MM, D.YY);
            } else {
                D1 = MakeDate(1, D.MM+1, D.YY);
            }
        }

    } else if ((D.MM == 4) || (D.MM == 6) || (D.MM == 9) || (D.MM == 11)) {
        if (D.DD < 30) {
            D1 = MakeDate(D.DD + 1, D.MM, D.YY);
        } else {
            D1 = MakeDate(1, D.MM+1, D.YY);
        }
    } else if (D.MM == 12) {
        if (D.DD < 31) {
            D1 = MakeDate(D.DD+1, D.MM, D.YY);
        } else {
            D1 = MakeDate(1, 1, D.YY+1);
        }
    }
    return (D1);
}

boolean IsDateValid (date D) {
    /* KAMUS LOKAL */

    /* ALGORITMA */
    if ((D.MM>12)||(D.MM<1)) {
        return (false);
    } else if (D.MM==2) {
        if (IsKabisat(D)) {
            return (D.DD<=29);
        } else {
            return (D.DD<=28);
        }
    } else if (((D.MM<=7)&&(D.MM % 2 == 1))||((D.MM>=8)&&(D.MM % 2 ==0))) {
        return (D.DD<=31);
    } else {
        return (D.DD<=30);
    }
}

date StrToDate (string str) {
    /* KAMUS LOKAL */
    int i;
    char x[3], y[3], z[5];
    /* ukuran dari string x dan y adalah 3 karakter, z adalah 5 karakter */

    /* ALGORITMA */
    i = 0;
    x[0] = str[i];
    i++;
    if (str[i] == '-') {
        x[1] = '\0';
        i++;
    } else {
        x[1] = str[i];
        i++;
        x[2] = '\0';
        i++;
    }

    y[0] = str[i];
    i++;
    if (str[i] == '-') {
        y[1] = '\0';
        i++;
    } else {
        y[1] = str[i];
        i++;
        y[2] = '\0';
        i++;
    }

    z[0] = str[i];
    i++;
    z[1] = str[i];
    i++;
    z[2] = str[i];
    i++;
    z[3] = str[i];
    i++;
    z[4] = '\0';

    if (str[i] == '\0') {
        return MakeDate(atoi(x), atoi(y), atoi(z));
    } else {
        return MakeDate(0, 0, 0);
    }
}

void DateToStr(date D, string res) {

    /* KAMUS LOKAL */
    int i;

    /* ALGORITMA */
    i = 0;
    if (D.DD > 9) {
        *(res + i) = IntToChar(D.DD/10);
        i++;
    }
    *(res + i) = IntToChar(D.DD % 10);
    i++;
    *(res + i) = '-';
    i++;

    if (D.MM > 9) {
        *(res + i) = IntToChar(D.MM/10);
        i++;
    }
    *(res + i) = IntToChar(D.MM % 10);
    i++;
    *(res + i) = '-';
    i++;

    if (D.YY > 999) {
        *(res + i) = IntToChar(D.YY/1000);
        i++;
    }
    if (D.YY > 99) {
        *(res + i) = IntToChar((D.YY/100) % 10);
        i++;
    }
    if (D.YY > 9) {
        *(res + i) = IntToChar((D.YY/10) % 10);
        i++;
    }
    *(res + i) = IntToChar(D.YY % 10);
    i++;
    *(res + i) = '\0';
}

void printDate(date dt) {
    /* KAMUS LOKAL */

    /* ALGORITMA */
    printf("%d-%d-%d", dt.DD, dt.MM, dt.YY);
}

date NOW() {
    /* KAMUS LOKAL */
    string strdate;
    char sd[3], sm[4], sy[5];
    int im;

    /* ALGORITMA */
    strcopy(strdate, __DATE__);
    sm[0] = strdate[0];
    sm[1] = strdate[1];
    sm[2] = strdate[2];
    sm[3] = '\0';
    sd[0] = strdate[4];
    sd[1] = strdate[5];
    sd[2] = '\0';
    sy[0] = strdate[7];
    sy[1] = strdate[8];
    sy[2] = strdate[9];
    sy[3] = strdate[10];
    sy[4] = '\0';

    if (strcompare(sm, "Jan") == 0) {
        im = 1;
    } else if (strcompare(sm, "Feb") == 0) {
        im = 2;
    } else if (strcompare(sm, "Mar") == 0) {
        im = 3;
    } else if (strcompare(sm, "Apr") == 0) {
        im = 4;
    } else if (strcompare(sm, "May") == 0) {
        im = 5;
    } else if (strcompare(sm, "Jun") == 0) {
        im = 6;
    } else if (strcompare(sm, "Jul") == 0) {
        im = 7;
    } else if (strcompare(sm, "Aug") == 0) {
        im = 8;
    } else if (strcompare(sm, "Sep") == 0) {
        im = 9;
    } else if (strcompare(sm, "Oct") == 0) {
        im = 10;
    } else if (strcompare(sm, "Nov") == 0) {
        im = 11;
    } else if (strcompare(sm, "Dec") == 0) {
        im = 12;
    }

    return MakeDate(atoi(sd), im, atoi(sy));
}
